home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 44
/
Amiga Format CD44 (1999-08-26)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-10].iso
/
-serious-
/
misc
/
calculator
/
gadgets.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-07-12
|
9KB
|
350 lines
/* Gadgets.c */
/* This file contains all the code for creation of the GUI and
code for handling interaction with it. */
#include <exec/types.h>
#include <clib/exec_protos.h>
#include <intuition/intuition.h>
#include <intuition/IntuitionBase.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/gfxmacros.h>
#include <graphics/GfxBase.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include "Calculator.h"
/*
** Here is where all the initialization and creation of GadTools gadgets
** take place. This function requires a pointer to a NULL-initialized
** gadget list pointer. It returns a pointer to the last created gadget,
** which can be checked for success/failure.
*/
struct Gadget *createAllGadgets(struct Gadget **glistptr, void *vi,
UWORD topborder, struct Gadget *my_gads[], struct StringInfo *gad_ptr[])
{
struct NewGadget ng;
struct Gadget *gad;
/* All the gadget creation calls accept a pointer to the previous gadget, and
** link the new gadget to that gadget's NextGadget field. Also, they exit
** gracefully, returning NULL, if any previous gadget was NULL. This limits
** the amount of checking for failure that is needed. You only need to check
** before you tweak any gadget structure or use any of its fields, and finally
** once at the end, before you add the gadgets.
*/
/* The following operation is required of any program that uses GadTools.
** It gives the toolkit a place to stuff context data.
*/
gad = CreateContext(glistptr);
/* Since the NewGadget structure is unmodified by any of the CreateGadget()
** calls, we need only change those fields which are different.
*/
ng.ng_LeftEdge = 10;
ng.ng_TopEdge = 10+topborder;
ng.ng_Width = 160;
ng.ng_Height = 12;
ng.ng_GadgetText = NULL;
ng.ng_TextAttr = &Topaz80;
ng.ng_VisualInfo = vi;
ng.ng_GadgetID = MYGAD_DISPLAY;
ng.ng_Flags = NG_HIGHLABEL;
// GT Tags whose type is unknown - 13 75 76
my_gads[MYGAD_DISPLAY] = gad = CreateGadget(NUMBER_KIND, gad, &ng,
// GTNM_Format, ?,
GTNM_Number, 0,
GTNM_MaxNumberLen, 10,
GTNM_Justification, GTJ_RIGHT,
GTNM_Border, TRUE,
GT_Underscore, '_',
TAG_END);
gad_ptr[0] = (struct StringInfo *) gad->SpecialInfo;
ng.ng_LeftEdge = 20;
ng.ng_TopEdge += 20;
ng.ng_Width = 20;
ng.ng_GadgetText = "9";
ng.ng_GadgetID = MYGAD_9;
my_gads[MYGAD_9] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "8";
ng.ng_GadgetID = MYGAD_8;
my_gads[MYGAD_8] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "7";
ng.ng_GadgetID = MYGAD_7;
my_gads[MYGAD_7] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 45;
ng.ng_GadgetText = "C";
ng.ng_GadgetID = MYGAD_CLEAR;
my_gads[MYGAD_CLEAR] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_TopEdge += 20;
ng.ng_LeftEdge -= 105;
ng.ng_GadgetText = "6";
ng.ng_GadgetID = MYGAD_6;
my_gads[MYGAD_6] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "5";
ng.ng_GadgetID = MYGAD_5;
my_gads[MYGAD_5] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "4";
ng.ng_GadgetID = MYGAD_4;
my_gads[MYGAD_4] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "*";
ng.ng_GadgetID = MYGAD_MULTIPLY;
my_gads[MYGAD_MULTIPLY] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "/";
ng.ng_GadgetID = MYGAD_DIVIDE;
my_gads[MYGAD_DIVIDE] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_TopEdge += 20;
ng.ng_LeftEdge -= 120;
ng.ng_GadgetText = "3";
ng.ng_GadgetID = MYGAD_3;
my_gads[MYGAD_3] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "2";
ng.ng_GadgetID = MYGAD_2;
my_gads[MYGAD_2] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "1";
ng.ng_GadgetID = MYGAD_1;
my_gads[MYGAD_1] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "+";
ng.ng_GadgetID = MYGAD_ADD;
my_gads[MYGAD_ADD] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "-";
ng.ng_GadgetID = MYGAD_SUBTRACT;
my_gads[MYGAD_SUBTRACT] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_TopEdge += 20;
ng.ng_LeftEdge -= 120;
ng.ng_GadgetText = ".";
ng.ng_GadgetID = MYGAD_POINT;
my_gads[MYGAD_POINT] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GA_Disabled, TRUE,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_GadgetText = "0";
ng.ng_GadgetID = MYGAD_0;
my_gads[MYGAD_0] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 30;
ng.ng_Width = 50;
ng.ng_GadgetText = "+/-";
ng.ng_GadgetID = MYGAD_CHANGE_SIGN;
my_gads[MYGAD_CHANGE_SIGN] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
ng.ng_LeftEdge += 60;
ng.ng_Width = 20;
ng.ng_GadgetText = "=";
ng.ng_GadgetID = MYGAD_EQUALS;
my_gads[MYGAD_EQUALS] = gad = CreateGadget(BUTTON_KIND, gad, &ng,
GT_Underscore, '_',
TAG_END);
return(gad);
}
/*
** Function to handle a GADGETUP and also vanilla keys event.
*/
BOOL handleGadgetEvent(struct Screen *mysc, struct Window *win, struct Gadget *gad, UWORD code,
struct Gadget *my_gads[], int& entered_number, int& no_figures)
{
switch (gad->GadgetID)
{
case MYGAD_9:
entered_number = 9;
no_figures++;
break;
case MYGAD_8:
entered_number = 8;
no_figures++;
break;
case MYGAD_7:
entered_number = 7;
no_figures++;
break;
case MYGAD_CLEAR:
entered_number = 0;
no_figures = 0;
GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], win, NULL, GTNM_Number, 0, TAG_END);
return(TRUE);
break;
case MYGAD_6:
entered_number = 6;
no_figures++;
break;
case MYGAD_5:
entered_number = 5;
no_figures++;
break;
case MYGAD_4:
entered_number = 4;
no_figures++;
break;
case MYGAD_MULTIPLY:
return(TRUE);
break;
case MYGAD_DIVIDE:
return(TRUE);
break;
case MYGAD_3:
entered_number = 3;
no_figures++;
break;
case MYGAD_2:
entered_number = 2;
no_figures++;
break;
case MYGAD_1:
entered_number = 1;
no_figures++;
break;
case MYGAD_ADD:
return(TRUE);
break;
case MYGAD_SUBTRACT:
return(TRUE);
break;
case MYGAD_POINT:
break;
case MYGAD_0:
if(no_figures == 0)
entered_number = 0;
else
entered_number = 10;
no_figures++;
break;
case MYGAD_CHANGE_SIGN:
return(TRUE);
break;
case MYGAD_EQUALS:
return(TRUE);
break;
}
switch(code)
{
case '9':
entered_number = 9;
no_figures++;
break;
case '8':
entered_number = 8;
no_figures++;
break;
case '7':
entered_number = 7;
no_figures++;
break;
case '6':
entered_number = 6;
no_figures++;
break;
case '5':
entered_number = 5;
no_figures++;
break;
case '4':
entered_number = 4;
no_figures++;
break;
case '*':
return(TRUE);
break;
case '/':
return(TRUE);
break;
case '3':
entered_number = 3;
no_figures++;
break;
case '2':
entered_number = 2;
no_figures++;
break;
case '1':
entered_number = 1;
no_figures++;
break;
case '+':
return(TRUE);
break;
case '-':
return(TRUE);
break;
case '.':
break;
case '0':
if(no_figures == 0)
entered_number = 0;
else
entered_number = 10;
no_figures++;
break;
}
return(FALSE);
}